using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using System.Globalization;
using System.Drawing;
using System.IO;
using BusinessTier.Facade.Interface;
using BusinessTier.Facade.Implementation;
using Domain;
using WebMVC.Models;
using Domain.Type;
using WebMVC.Helpers;



namespace WebMVC.Controllers
{
    public class StaticDataController : ControllerBase
    {
        
        private IStaticDataService staticDataService;

        public StaticDataController()
            : this(new StaticDataService())
        {
           
        }

        public StaticDataController(IStaticDataService staticDataService)
        {
            this.staticDataService = staticDataService;
        }

        public ActionResult GetImageFromText(string text, int fontId, string color, float size)
        {
            //Domain.Font fontDo = staticDataService.GetFontById(fontId);

            System.Drawing.Font objFont = new System.Drawing.Font("Arial", size, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel);
            MemoryStream textImage = (new TextImageGenerator()).GetImageFromText(text, objFont, color);
            return new FileContentResult(textImage.ToArray(), "image/png");
        }

        [ChildActionOnly]
        [OutputCache(Duration = int.MaxValue, VaryByParam = "none")]
        public ActionResult StyleMenuViewControl()
        {
            List<StyleMenuItemViewModel> listItems = new List<StyleMenuItemViewModel>();
            foreach (var styleItem in staticDataService.GetAllStyles().ToList())
            {
                int count = staticDataService.GetProductCountForStyle(styleItem.StyleId, ProductTypeCode.BUSINESS_CARD);
                if (count != 0)
                {
                    StyleMenuItemViewModel styleViewItem = new StyleMenuItemViewModel() 
                    {
                        Id = styleItem.StyleId,
                        Name = GetLocalizedName(styleItem),
                        ProductsCount = count
                    };
                    listItems.Add(styleViewItem);
                }
            }
            return PartialView(listItems);
        }

        [ChildActionOnly]
        [OutputCache(Duration = int.MaxValue, VaryByParam = "none")]
        public ActionResult IndustryMenuViewControl()
        {
            List<IndustryMenuItemViewModel> listItems = new List<IndustryMenuItemViewModel>();
            foreach (var industryItem in staticDataService.GetAllIndustries().ToList())
            {
                int count = staticDataService.GetProductCountForIndustry(industryItem.IndustryId, ProductTypeCode.BUSINESS_CARD);
                if (count != 0)
                {
                    IndustryMenuItemViewModel industryViewItem = new IndustryMenuItemViewModel() 
                    {
                        Id = industryItem.IndustryId,
                        Name = GetLocalizedName(industryItem),
                        ProductsCount = count
                    };
                    
                    listItems.Add(industryViewItem);
                }
            }
            return PartialView(listItems);
        }

        [ChildActionOnly]
        [OutputCache(Duration = int.MaxValue, VaryByParam="none")]
        public ActionResult ColorListViewControl()
        {
            List<ColorItemViewModel> listItems = new List<ColorItemViewModel>();
            foreach (var colorItem in staticDataService.GetAllColorThemes().ToList())
            {
                int count = staticDataService.GetProductCountForColor(colorItem.ColorThemeId, ProductTypeCode.BUSINESS_CARD);
                if (count != 0)
                {
                    ColorItemViewModel colorViewItem = new ColorItemViewModel() 
                    { 
                        Id = colorItem.ColorThemeId,
                        RGBCode = colorItem.RGBHex,
                        Name = GetLocalizedName(colorItem),
                    };
                    listItems.Add(colorViewItem);
                }
            }
            return PartialView(listItems);
        }
    }
}
